package com.service.dwm;

import com.dao.BaseDao;
import com.entity.CatPost;
import com.entity.PageManager;
import com.entity.User;
import com.entity.Varietiess;


import java.io.File;
import java.util.List;

public class VarietiesService extends BaseDao {

    /*public Object[] getObjArr(User user){
        Object[] params = {user.getUsername(), user.getPassword(), user.getName(),
        user.getSex(), user.getAge(), user.getBirthday(), user.getPhone(), user.getHeight(),
        user.getPic()};
        return params;
    }*/

    /**
     * 增加用户数据
     * @param varieties
     * @return
     */
    public boolean addVarieties(Varietiess varieties) {
        /*String sql = "INSERT INTO `rz1217`.`user`(`username`, `password`, " +
                "`name`, `sex`, `age`, `birthday`, `phone`, " +
                "`height`, `pic`) " +
                "VALUES (?,?,?,?,?,?,?,?,?)";
        Object[] params = getObjArr(user);*/
        try {
            return insert(varieties) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 分页查询
     * @param pm
     * @return
     */
    public List<Varietiess> queryVarieties(PageManager pm) {
        int start = (pm.getPn() - 1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from Varietiess limit ?,?";
        return findList(Varietiess.class, sql, start, end);
    }

    /**
     * 查询全部
     * @return
     */
    public List<Varietiess> queryVarietiess() {
        String sql = "select * from Varietiess";
        return findList(Varietiess.class, sql);
    }

    /**
     * 按品种查询
     * @return
     */
    public List<CatPost> queryCatPosts(String catbreed,PageManager pm) {
        int start = (pm.getPn() - 1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from catpost where catbreed = ? and state ='T' limit ?,?";
        return findList(CatPost.class, sql,catbreed, start, end);
    }


    /**
     * 按条件查询
     * @param pm
     * @return
     */
    public Varietiess queryVarieties(String pm) {

        try {
            return (Varietiess) queryVarieties(new Varietiess(pm));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 分页查询
     * @return
     */
//    public List<Knowlede> queryVarieties() {
//        String sql = "select * from Varieties";
//        return findList(Knowlede.class, sql);
//    }

    /**
     * 查询总数据条
     * @param
     * @return
     */
    public int queryVarietiesTotalCount() {
        String sql = "select count(*) from Varietiess";
        return count(sql);
    }


    public boolean delVarieties(String varieties) {
        Varietiess varietiess = queryVarieties(varieties);
        boolean result = false;
        try {
            result = deleteVarietiess(new Varietiess(varieties))> 0;
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean updateVarieyies(Varietiess varietiess) {
        Varietiess varieties = queryVarieties(varietiess.getVarieties());
        try {
            boolean result = updateVarieties(varietiess) > 0;
            //修改成功，并且传递的图片和数据库的图片不同时，才删除图片文件
            if (result && varieties.getPic1() != null
                    && !varieties.getPic1().equals(varietiess.getPic1())) {
                //删除原图片
                new File(varieties.getPic1()).delete();
            }
            if (result && varieties.getPic2() != null
                    && !varieties.getPic2().equals(varietiess.getPic2())) {
                //删除原图片
                new File(varieties.getPic2()).delete();
            }
            if (result && varieties.getPic3() != null
                    && !varieties.getPic3().equals(varietiess.getPic3())) {
                //删除原图片
                new File(varieties.getPic3()).delete();
            }
            if (result && varieties.getPic4() != null
                    && !varieties.getPic4().equals(varietiess.getPic4())) {
                //删除原图片
                new File(varieties.getPic4()).delete();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}